// types/list.d.ts
interface ListItem {
  id: string
  title: string
  cover: string
  description: string
  createdAt: string
  views: number
}

type LoadingState = 'loading' | 'success' | 'no-more' | 'error';

import { STORAGE_KEY } from "../../../api/config";
import {page,Vote,VoteOption} from "../../../api/votes"
import {request,RequestConfig,HttpRequestMethod,LengyuHttpResponse,PageRequestParam,download,upload}  from '../../../utils/request'
// pages/list/list.ts
Page({
  data: {
    items: [],
    page: 1,
    loadingState: 'loading',
    isRefreshing: false,
    //1:我发起的2：我参与的
    searchType:1
  },

  // onLoad() {
  //   this.loadData()
  // },
  /**
  * 生命周期函数--监听页面加载
  */
 onLoad(option: any) {
    console.log(option)
    if(option.searchType){
      this.setData({
        searchType: option.searchType
      })
    }
    else{
      this.setData({
        searchType: 1
      })
    }
   
    this.loadData()
  },
  async loadData(isRefresh = false) {
    const newPage = isRefresh ? 1 : this.data.page
    this.setData({
      loadingState: 'loading',
      isRefreshing: isRefresh
    })

    try {
      let param:PageRequestParam={
        pageIndex:newPage,
        pageSize:10,
        paramMapObj:{
          searchType:this.data.searchType
        }
      }
  
      const newItems = await page(param)
      console.log("查询结果",newItems)
      this.setData({
        items: isRefresh ? newItems.data.records : [...this.data.items, ...newItems.data.records],
        page: newPage + 1,
        loadingState: newItems.data.records.length ? 'success' : 'no-more'
      })
      console.log("处理结果",this.data.items)
    } catch (error) {
      this.setData({ loadingState: 'error' })
    } finally {
      this.setData({ isRefreshing: false })
    }
  },

  onReachBottom() {
    if (this.data.loadingState !== 'no-more') {
      this.loadData()
    }
  },

  onPullDownRefresh() {
    this.loadData(true)
    wx.stopPullDownRefresh()
  },

  handleItemTap(e: WechatMiniprogram.TapEvent) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/vote/index/index?id=${id}`,
      success: () => {
        // wx.vibrateShort()
      }
    })
  }
})
